// pages/home/home.js
const app = getApp();
Component({
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      app.pages.add(this)
      
      // 给html用
      this.setData({
        host: app.globalData.host
      })
      // 获取数据，默认无类型，按时间倒序
      this.refreshItemList()
    }
  },

  pageLifetimes: {
    show() {
      // 设置 自定义tabbar 此时的下标
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    category: 'ALL',
    orderType: 'TIME_NEW',
    word: '',
    // 当前页数
    pages: 1,
    // 种类菜单 item
    categoryItem: [{
        text: '全部商品',
        value: 'ALL'
      },
      {
        text: '书籍',
        value: 'BOOK'
      },
      {
        text: '体育娱乐',
        value: 'SPORT'
      },
      {
        text: '日常生活',
        value: 'DAILY'
      },
      {
        text: '化妆护肤',
        value: 'COSMETICS'
      },
      {
        text: '衣物',
        value: 'CLOTHES'
      },
      {
        text: '其他',
        value: 'OTHER'
      },
    ],
    // 排序菜单 item
    orderItem: [{
        text: '最新优先',
        value: 'TIME_NEW'
      },
      {
        text: '价格由低到高',
        value: 'PRICE_ASC'
      },
      {
        text: '价格由高到低',
        value: 'PRICE_DESC'
      },
    ],
    cardList: [],
    // 选中的下标
    cardIndex: -1,
    // 自定义顶部条的高度, 也是搜索框距离顶部的高度
    stickyTop: app.globalData.CustomBar,
    // 用于显示加载条 0,下拉刷新 1，加载中 2，到底了
    loadingState: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 下拉刷新
    onReachBottom: function() {
      if(this.data.loadingState != 2){
        this.setData({ loadingState: 1 })
        console.log("reach bottom")
        // TODO 添加发送请求获取后面的元素
        this.moreItemList()
      }
    },

    /**
     * 刷新 item
     */
    refreshItemList() {
      let host = app.globalData.host
      let category = this.data.category
      let orderType = this.data.orderType
      let word = this.data.word
      let pageNum = 1

      wx.request({
        url: host + '/item',
        data: {
          category: category,
          orderType: orderType,
          word: word,
          pageNum: pageNum
        },
        success: (res) => {
          this.updateLoadingState(res)
          let newItemList = res.data.data.records
         // let pageNum = res.data.data.pages
          this.setData({
            cardList: newItemList,
            pages: 1
          })
        }
      })
    },

    /**
     * 获取更多 item
     */
    moreItemList() {
      let host = app.globalData.host
      let category = this.data.category
      let orderType = this.data.orderType
      let word = this.data.word
      let pageNum = this.data.pages

      wx.request({
        url: host + '/item',
        data: {
          category: category,
          orderType: orderType,
          word: word,
          pageNum: pageNum+1
        },
        success: (res) => {
          this.updateLoadingState(res)
          let pages= res.data.data.pages
          let newItemList = res.data.data.records
          let oldItemList = this.data.cardList
          let itemList = oldItemList.concat(newItemList)
          this.setData({
            cardList: itemList,
            pages : pages
          })
        }
      })
    },

  
    /**
     * 排序类型变换
     */
    orderTypeChange(e) {
      this.setData({
        orderType: e.detail
      })
      this.refreshItemList()
    },

    categoryChange(e) {
      this.setData({
        category: e.detail
      })
      this.refreshItemList()
    },

    wordChange(e) {
      console.log(e)
      this.setData({
        word: e.detail.value
      })
    },

    // 点击搜索
    search() {
      this.refreshItemList()
    },

    // 更新 loading state
    updateLoadingState(res){
      let length = res.data.data.records.length
      if(length < 30){
        this.setData({loadingState: 2})
      }else{
        this.setData({loadingState: 0})
      }
    },

    navigateToItemPage(e) {
      this.setData({cardIndex: e.currentTarget.dataset.index})
      wx.navigateTo({
        url: '/pages/item/item',
      })
    }  
  },

 

})